package com.treeroot.orderservice.order.controller;



import com.treeroot.commonutils.AOP.Limit;
import com.treeroot.commonutils.result.R;
import com.treeroot.commonutils.utils.JwtUtils;
import com.treeroot.orderservice.order.entity.PcOrder;
import com.treeroot.orderservice.order.service.PcOrderService;
import com.treeroot.servicebase.config.ApiVersion;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author TreeRoot
 * @since 2022-05-18
 */
@RestController
@Api(description = "订单")
@RequestMapping("/order/pcorderinfo")
@ApiVersion(1.0)
@CrossOrigin

public class PcOrderController {
    @Autowired
    PcOrderService orderService;

    /**
     * 根据token拿到用户id 通过用户id远程调用用户信息模块
     * 根据当前获取到的航班id 远程调用航班信息模块
     * 根据 用户信息 还有航班信息建立订单  返回订单号
     *
     * @param planeId  航班信息id
     */
    @RequestMapping("createBaseOrder/{planeId}/{planeType}")
    @Limit(key = "updatePlaneByIdOrder", permitsPerSecond = 1, timeout = 100, msg = "当前排队人数较多，请稍后再试！")
    public R createOrder(@PathVariable String planeId,@PathVariable String planeType, HttpServletRequest request) {
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        String orderNo = orderService.createBaseOrder(planeId, planeType,userId);
        return R.ok().data("orderNo", orderNo);
    }


    /**
     * 根据订单id查询订单信息
     * @param orderId 订单id
     */

    @GetMapping("getOrderInfo/{orderId}")
    @Limit(key = "updatePlaneByIdOrder", permitsPerSecond = 1, timeout = 100, msg = "当前排队人数较多，请稍后再试！")
    public R getOrderInfo(@PathVariable String orderId) {
        PcOrder orderNo =    orderService.getOrderInfo(orderId);
        return R.ok().data("item", orderNo);
    }

    @PostMapping("/orderPage/{current}/{limit}")
    public R getOrderPage(@PathVariable long current, @PathVariable long limit
                      ) {

        Map map = orderService.getOrderList(current,limit);
        return R.ok().data(map);
    }


    /**
     * 订单记录
     */
    @PostMapping("/order/{current}/{limit}")
    public R getOrder(@PathVariable long current, @PathVariable long limit,
                      @RequestBody(required = false) PcOrder order) {

        Map map = orderService.getQueryOrderList(current,limit,order);
        return R.ok().data(map);
    }


    /**
     * echarts管理 订单总数量统计
     */
    @GetMapping("count")
    public  R count (){
        return R.ok().data("orderCount",orderService.count(null));
    }


}



